package com.ice.crm.user.controller;

import com.ice.crm.user.model.User;
import com.ice.crm.user.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService userService;


    /**
     * 扫描根路径的方法【扫描浏览器根路径下的 cookie 内容】
     *
     * @return {@link String}
     */
    @RequestMapping("/")
    public String index(HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "index";
        }
        String loginAct = "";
        String loginPwd = "";
        for (Cookie cookie : cookies) {
            // 将浏览器中的信息赋值给 定义的变量
            if (cookie.getName().equals("loginAct")) {
                loginAct = cookie.getValue();
            }
            // 将浏览器中的信息赋值给 定义的变量
            if (cookie.getName().equals("loginPwd")) {
                loginPwd = cookie.getValue();
            }
        }
        // 需要账号和密码同时存在才可以，否则跳转到登录页面
        if (loginAct == null || loginPwd == null) {
            return "index";
        }
        // 调用 login 的方法，实现自动登录
        String result = login(loginAct, loginPwd, model, session, "yes", response);
        return result;
    }

    /*
     * 登录的方法
     *
     * @param loginAct
     * @param loginPwd
     * @param model
     * @param session
     * @param isAutoLogin
     * @param response
     * @return {@link String}
     */
    @RequestMapping("/login")
    public String login(String loginAct, String loginPwd, Model model, HttpSession session, String isAutoLogin, HttpServletResponse response) {
        User user = new User();
        user.setLoginAct(loginAct);
        user.setLoginPwd(loginPwd);

        int result = userService.login(user);
        switch (result) {
            case 1:
            case 2:
                model.addAttribute("errMsg", "账号密码不匹配！~");
                return "index";
            case 3:
                model.addAttribute("errMsg", "账号过期");
                return "index";
            case 4:
                model.addAttribute("errMsg", "账号冻结");
                return "index";
        }
        session.setAttribute("userSession", user);
        // 如果等于 null 表示没有勾选记住我，跳转 /main 页面
        if (isAutoLogin == null) {
            return "redirect:/main";
        }
        // 到了这里表示 勾选了记住我
        // 获取要存入 cookie 的数据
        Cookie cookieLoginAct = new Cookie("loginAct", user.getLoginAct());
        Cookie cookieLoginPwd = new Cookie("loginPwd", user.getLoginPwd());
        // 设置路径
        cookieLoginAct.setPath("/");
        cookieLoginPwd.setPath("/");
        // 设置超时时间
        cookieLoginAct.setMaxAge(60 * 60 * 24 * 30);
        cookieLoginPwd.setMaxAge(60 * 60 * 24 * 30);
        // 将信息添加到浏览器中
        response.addCookie(cookieLoginAct);
        response.addCookie(cookieLoginPwd);

        return "redirect:/main";
    }

    /**
     * 重定向跳转主页的方法
     *
     * @return {@link String}
     */
    @RequestMapping("/main")
    public String main() {
        return "workbench/main/index";
    }
}
